from time import sleep

import requests
import pytest

from api import log
from api.api_register_login import ApiRegisterLogin

from tools import common_assert, read_json, clear_data


class TestRegisterLogin:

    @classmethod
    def setup_class(cls):
        log.info("正在清除数据")
        clear_data()
        log.info("数据清理成功")
    @pytest.fixture(autouse=True)
    #初始化
    def setup(self):
        #获取session
        self.session = requests.session()
        log.info("正在获取session:{}".format(self.session))
        #创建api实例对象
        self.api=ApiRegisterLogin(self.session)
        log.info("正在获取ApiRegisterLogin 对象:{}".format(self.api))
        #结束
    def tearDown(self):
        #关闭session
        self.session.close()
        log.info("关闭session对象:{}".format(self.session))

        # 图片验证码接口测试
    @pytest.mark.parametrize(["random","expect_code",],read_json("register_login.json","img_code"))
    def test01_img_code(self,random,expect_code):
        try:
            result=self.api.api_img_code(random)
            log.info("正在获取图片验证码接口测试的结果为：{}".format(result.status_code))
            common_assert(result,expect_code)
            log.info("图片验证码断言通过")
            print(result.status_code)
        except Exception as e:
            log.error(e)
            #抛异常
            raise
        # 手机验证码接口测试

    @pytest.mark.parametrize(["phone","imgVerifyCode","expect_json"], read_json("register_login.json", "phone_code"))
    def test02_phone_code(self,phone,imgVerifyCode,expect_json):
        try:
            #请求图片验证码
            self.api.api_img_code(123123)
            #调用手机短信接口
            result=self.api.api_phone_code(phone,imgVerifyCode)
            log.info("正在获取手机验证码接口的结果为：{}".format(result.text))
            common_assert(result,json_desc=expect_json)
            log.info("手机验证码断言通过")
            print(result.json)
        except Exception as e:
            log.error(e)
            #抛异常
            raise

        # 注册接口 测试
    @pytest.mark.parametrize(["phone","password","VerifyCode","phone_code","expect_json"], read_json("register_login.json", "register"))
    def test03_register(self,phone,password,VerifyCode,phone_code,expect_json):
        try:
        #获取图片验证码
            self.api.api_img_code(123123)
            #获取手机验证码
            self.api.api_phone_code(phone, VerifyCode)
            result=self.api.api_register(phone,password,VerifyCode,phone_code)
            log.info("正在获取注册接口的结果为：{}".format(result.text))
            print(result.json())
            common_assert(result, json_desc=expect_json)
            log.info("注册断言通过")
        except Exception as e:
            log.error(e)
            raise
        # 登录接口 测试
    @pytest.mark.parametrize(["keywords","password","expect_json"], read_json("register_login.json", "login"))
    def test04_login(self,keywords,password,expect_json):
        try:
           i=1
           result=None
           #判定数据是否有error123，则为测试锁定
           if password=="error123":
               while i<=3:
                   result=self.api.api_login(keywords, password)
                   log.info("登录接口的结果为：{}".format(result.text))
                   i+=1
               assert "已被锁定" in result.text
               log.info("登录->锁定，断言成功")
               #锁定60s
               sleep(60)
               #登录成功
               result=self.api.api_login("13011113333","qq123456")
               log.info("登录接口的结果为：{}".format(result.text))
               common_assert(result, json_desc=expect_json)
               log.info("登录断言成功")
           else:
               result= self.api.api_login(keywords,password)
               log.info("登录接口的结果为：{}".format(result.text))
               common_assert(result,json_desc=expect_json)
               log.info("登录断言成功")
               print(result.json())
        except Exception as e:
            log.error(e)
            raise
        # 登录查询接口 测试

    @pytest.mark.parametrize(["status","expect_json"], read_json("register_login.json", "login_status"))
    def test05_login_staus(self ,status,expect_json):
        try:
            if status =="已登录":
                # "调用登录接口"
                self.api.api_login(13600001121,"test123")
            result=self.api.api_login_status()
            log.info("登录查询接口的结果为：{}".format(result.text))
            common_assert(result,json_desc=expect_json)
            print(result.json())
        except Exception as e:
            log.error(e)
            raise
